package cn.anzhiyublog.staranimegallery.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * Caffeine本地缓存配置
 *
 * @author 安知一只鱼
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "caffeine")
public class CaffeineConfig {
    /**
     * 初始容量
     */
    private int initialCapacity;

    /**
     * 最大容量
     */
    private long maximumSize;

    /**
     * 过期时间（分）
     */
    private int timeout;

    @Bean(name = "caffeine")
    public Cache<String,String> caffeine(){
        return Caffeine.newBuilder()
                .initialCapacity(initialCapacity)
                .maximumSize(maximumSize)
                .expireAfterWrite(timeout, TimeUnit.MINUTES)
                .build();
    }
}
